package com.yonyou.pmclouds.workstartreport;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.annotation.Insert;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.service.CommonPropertiesProcessor;
import com.yonyou.pmclouds.outter.enterprise.EnterpriseInfoQuery;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.team.rmiitf.ProjectRefWithTeamQuery;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import com.yonyou.pmclouds.workquality.entity.WorkqualityCheckTypeEnum;
import com.yonyou.pmclouds.workquality.entity.WorkqualityDetailVO;
import com.yonyou.pmclouds.workquality.entity.WorkqualityVO;
import com.yonyou.pmclouds.workquality.service.rmiitf.WorkqualityQueryService;
import com.yonyou.pmclouds.workstartreport.entity.WorkStartCheckVO;
import com.yonyou.pmclouds.workstartreport.entity.WorkStartVO;
import com.yonyou.pmclouds.workstartreport.rmiitf.WorkStartMaintain;
import com.yonyou.pmclouds.workstartreport.rmiitf.WorkStartQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 开工报告
 *
 */
@Slf4j
@RequestMapping({"/mobile/workStart","/workStart"})
@RestController
public class WorkStartController {
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private WorkStartMaintain workStartMaintain;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private WorkStartQuery workStartQuery;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ProjectRefWithTeamQuery query;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private WorkqualityQueryService queryService;
    @Autowired
    private CommonPropertiesProcessor propertiesProcessor;
    @Autowired
    private UserInfoQuery userQuery;
    @Autowired
    private EnterpriseInfoQuery enterpriseInfoQuery;

    private static final String[] USER_ID_FIELDS = {WorkStartVO.CREATOR};
    private static final String[] USER_NAME_FIELDS = {WorkStartVO.CREATOR_NAME};

    /**
     * 添加开工报告
     * @param workStartVO
     * @return
     * @throws BusinessException
     */
    @Insert
    @PMApprove
    @RequestMapping("/add")
    public WorkStartVO add(@RequestBody WorkStartVO workStartVO) throws BusinessException {
        //补全开工报告单据类型字段与单据状态字段
        wrapWorkStartVO(workStartVO);
        //调用Service层的添加方法
        WorkStartVO insert = workStartMaintain.insert(workStartVO);
        return insert;
    }



    /**
     * 修改开工报告
     * @param workStartVO
     * @return
     * @throws BusinessException
     */
    @PMApprove(trigger = "commit")
    @RequestMapping("/update")
    public WorkStartVO update(@RequestBody WorkStartVO workStartVO) throws BusinessException {
        //直接调用Service层的update方法
        WorkStartVO update = workStartMaintain.update(workStartVO);
        //为返回的对象追加创建者姓名这个属性
        userQuery.tranUserInfoField(new WorkStartVO[]{update}, USER_ID_FIELDS, USER_NAME_FIELDS);
        return update;
    }

    /**
     * 删除开工报告
     * @param workStartVO
     * @throws BusinessException
     */
    @RequestMapping("/remove")
    public void remove(@RequestBody WorkStartVO workStartVO) throws BusinessException {
            workStartMaintain.delete(workStartVO);
    }

    /**
     * 通过项目ID查询所有条件,用于项目单据列表展示
      * @param projectId
     * @return
     * @throws BusinessException
     */
    @RequestMapping("/getAllConditionsByProjectId")
   public Object getAllConditionsByProjectId(String projectId) throws BusinessException {
        //调用Service层的方法
        WorkStartVO[] allCondition = workStartQuery.getAllCondition(projectId);
        //判断该项目下是否存在单据
        if (allCondition == null || allCondition.length == 0) {
            return null;
        }
        //为返回的对象追加上创建者姓名属性值
        userQuery.tranUserInfoField(allCondition, USER_ID_FIELDS, USER_NAME_FIELDS);
        //为返回的对象追加上施工方单位的名称
        enterpriseInfoQuery.tranEntInfoField(allCondition, new String[]{WorkStartVO.PK_ENTERPRISE}, new String[]{WorkStartVO.ENTERPRICE_NAME});
        //包装成另外的对象再返回，其追加了评论数这个字段以及值
        return propertiesProcessor.wrapAdditionalProps(allCondition);
    }


    /**
     * 通过报告ID查询开工报告
     * @param reportId
     * @return
     * @throws BusinessException
     */
     @RequestMapping("/getReportById")
    public Object getReportByProjectId(String reportId) throws BusinessException {
        //调用Service的方法
         WorkStartVO project = workStartQuery.getReport(reportId);
         //追加创建者姓名
         userQuery.tranUserInfoField(new WorkStartVO[]{project}, USER_ID_FIELDS, USER_NAME_FIELDS);
         //追加施工单位名称以及监理单位名称
         enterpriseInfoQuery.tranEntInfoField(new WorkStartVO[]{project}, new String[]{WorkStartVO.PK_ENTERPRISE}, new String[]{WorkStartVO.ENTERPRICE_NAME});
         enterpriseInfoQuery.tranEntInfoField(new WorkStartVO[]{project}, new String[]{WorkStartVO.PK_SUPERVISION}, new String[]{WorkStartVO.SUPERVISION_NAME});
         return project;
     }


    /**
     * 通过检查项ID查询检查项
     * @param checkId
     * @retu00rn
     * @throws BusinessException
     */
    @RequestMapping("/getByCheckId")
    public Object getByCheckId(String checkId) throws BusinessException {
        WorkStartCheckVO check = workStartQuery.getCheck(checkId);
        return check;
    }
    /**
     * 补充单据类型信息
     * @param vo
     */
    private void wrapWorkStartVO(WorkStartVO vo){
        if(vo == null) return;
        //单据类型 类型‘KGBG’
        vo.setBillType(BillTypeConst.WORKSTART_BILLTYPE);
        if(StringUtils.isEmpty(vo.getPkTenant())){
            vo.setPkTenant(RuntimeEnvironment.getTenantId());
        }
        //单据状态 添加为自由态5
        vo.setBillstatus(BillStatusConst.COMMON_FREE_STATUS);
    }


}
